package com.river.config;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by jl on 2018/9/4 0004
 * 利用装饰者模式，增强request 的getParameter功能
 */
public class RiverFilter implements Filter {
    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String charset = filterConfig.getInitParameter("charset");
        System.out.println("web.xml中配置的字符编码：" + charset);
        System.out.println("进行getParameter增强...");

        RiverRequest riverRequest = new RiverRequest(request);
        filterChain.doFilter(riverRequest, response);
    }

    @Override
    public void destroy() {

    }
}
